package cc.controller;

import cc.eguid.commandManager.RstpToRtmpUtil;
import cc.eguid.commandManager.data.CommandTasker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @desc: 回调通知
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/10/09 16:50
 */
@RestController
@RequestMapping("/v1/rtmp/")
public class RTMPNotifyController {

    private static Logger log = LoggerFactory.getLogger(RTMPNotifyController.class);
    /**
     * 推流到播放步骤
     * 1，on_connect
     * 2, on_publish
     * 3, on_play
     */
    /**
     * 连接回调方法
     * @param request
     * @return
     */
    @GetMapping(value = "/on_connect")
    public String onConnect(HttpServletRequest request) {
        debug(request, "on_connect");
        return "on_connect";
    }

    /**
     * 播放回调方法
     * @param request
     * @return
     */
    @GetMapping(value = "/on_play")
    public String onPlay(HttpServletRequest request) {
        String id = request.getParameter("name");
        debug(request, "on_play");
        CommandTasker tasker = RstpToRtmpUtil.getCommandManager().query(id);
        if (null != tasker) {
            if(null == tasker.getOnline()||tasker.getOnline()==0){
                tasker.setOnline(1);
            }else{
                tasker.setOnline(tasker.getOnline()+1);
            }
            System.out.println(id+"当前播放在线："+ RstpToRtmpUtil.getCommandManager().query(id).getOnline());
        }

        return "on_play";
    }

    /**
     * 发布回调
     * @param request
     * @return
     */
    @GetMapping(value = "/on_publish")
    public String onPublish(HttpServletRequest request) {
        debug(request, "on_publish");
        return "on_publish";
    }

    /**
     * 推流结束回调
     * @param request
     * @return
     */
    @GetMapping(value = "/on_done")
    public String onDone(HttpServletRequest request) {
        debug(request, "on_done");
        return "on_done";
    }

    /**
     * 播放停止回调
     * @param request
     * @return
     */
    @GetMapping(value = "/on_play_done")
    public String onPlayDone(HttpServletRequest request) {
        String id = request.getParameter("name");
        if (null != id && !"".equals(id)) {
            CommandTasker tasker = RstpToRtmpUtil.getCommandManager().query(id);
            if (null != tasker) {
                if(tasker.getOnline()>1){
                    log.info("任务：{}当前播放在线{},不关闭推流.",id,tasker.getOnline());
                    tasker.setOnline(tasker.getOnline()-1);
                }else{
                    log.info("任务：{}停止.",id);
                    RstpToRtmpUtil.stop(id);
                }
            }
        }
        // debug(request, "on_play_done");
        return "on_play_done";
    }

    /**
     * 推流发布结束回调
     * @param request
     * @return
     */
    @GetMapping(value = "/on_publish_done")
    public String onPublishDone(HttpServletRequest request) {
        debug(request, "on_publish_done");
        return "on_publish_done";
    }

   /* @GetMapping(value = "/on_record_done")
    public String onRecordDone(HttpServletRequest request) {
        debug(request, "on_record_done");
        return "on_record_done";
    }

    @GetMapping(value = "/on_update")
    public String onUpdate(HttpServletRequest request) {
        //debug(request, "on_update");
        return "on_update";
    }*/

    private String debug(HttpServletRequest request, String action) {
        String str = action + ": " + request.getRequestURI() + " " + request.getQueryString();
        System.out.println(str);
        return str;
    }
}
